﻿@using MUnique.OpenMU.Persistence
@typeparam TItem

<EditForm Model="@Item">
    <CascadingValue Value="@this.PersistenceContext">
        <DataAnnotationsValidator />
        <CreationAutoFields />

        <ValidationSummary />
    </CascadingValue>
    <button type="submit" @onclick="@Submit" class="btn-primary">Submit</button>
    <button @onclick="@Cancel" class="btn-secondary">Cancel</button>
</EditForm>

@code {

    /// <summary>
    /// Gets or sets the modal instance.
    /// </summary>
    [CascadingParameter]
    BlazoredModalInstance BlazoredModal { get; set; } = null!;

    /// <summary>
    /// Gets or sets the item which should be created.
    /// </summary>
    [Parameter]
    public TItem Item { get; set; } = default!;

    /// <summary>
    /// Gets or sets the persistence context which should be used. It's required for lookups.
    /// </summary>
    [Parameter]
    public IContext PersistenceContext { get; set; } = null!;

    private void Submit()
    {
        this.BlazoredModal.Close(ModalResult.Ok(Item));
    }

    private void Cancel()
    {
        this.BlazoredModal.Cancel();
    }
}
